<template>
  <div :id="id" :style="{ height:height,width:width }" />
</template>

<script>
import "echarts-wordcloud/dist/echarts-wordcloud";
import "echarts-wordcloud/dist/echarts-wordcloud.min";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    fontMinSize: {
      type: String,
      default: "8"
    },
    fontMaxSize: {
      type: String,
      default: "28"
    },
    id: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    wordData: {
      type: Array,
      default: () => [
  {
    name: "直播带货",
    value: 354
  },
  {
    name: "网红直播",
    value: 171
  },
  {
    name: "双十一",
    value: 220
  },
  {
    name: "双十二",
    value: 143
  },
  {
    name: "直播争议",
    value: 95
  },
  {
    name: "国潮",
    value: 256
  },
  {
    name: "纽约时装周",
    value: 198
  },
  {
    name: "汉服热",
    value: 364
  },
  {
    name: "富贵鸟破产",
    value: 59
  },
  {
    name: "杰克沃克破产",
    value: 62
  },
  {
    name: "堡狮龙亏损",
    value: 39
  },
        {
          name: "线下门店减少",
          value: 266
        },
        {
          name: "疫情影响",
          value: 320
        },
        {
          name: "品牌融合",
          value: 199
        },
        {
          name: "运动时尚",
          value: 338
        },
        {
          name: "炒鞋",
          value: 276
        },
        {
          name: "维密秀取消",
          value: 65
        },
        {
          name: "运动品牌内衣",
          value: 103
        },
        {
          name: "可持续时尚",
          value: 326
        },
        {
          name: "环保问题",
          value: 112
        },
        {
          name: "原料改善",
          value: 91
        },
        {
          name: "市场下沉",
          value: 286
        },
        {
          name: "电商环境",
          value: 283
        },
        {
          name: "线上购物",
          value: 204
        },
        {
          name: "卡尔离世",
          value: 187
        },
        {
          name: "薇娅卖火箭",
          value: 231
        },
        {
          name: "董明珠直播",
          value: 159
        },
        {
          name: "明星进入直播",
          value: 264
        },
        {
          name: "数字化转型",
          value: 338
        },
        {
          name: "数据挖掘",
          value: 281
        },
        {
          name: "数据应用",
          value: 123
        },
        {
          name: "大数据分析",
          value: 290
        },
        {
          name: "智能工厂",
          value: 319
        },
        {
          name: "视频平台",
          value: 59
        },
        {
          name: "小众服饰",
          value: 84
        },
        {
          name: "李宁翻身",
          value: 68
        },
        {
          name: "太平鸟营收",
          value: 37
        },
        {
          name: "文化自信",
          value: 51
        },
        {
          name: "快时尚品牌",
          value: 146
        }
]
    },
    title: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      let echarts = require('echarts');
      this.chart = echarts.init(document.getElementById(this.id));
      const option = {
        title: {
          text: this.title,
          x: "center"
        },
        tooltip: {
          pointFormat: "{series.name}: <b>{point.percentage:.1f}%</b>"
        },
        series: [
          {
            type: "wordCloud",
            //用来调整词之间的距离
            gridSize: 10,
            //用来调整字的大小范围
            // Text size range which the value in data will be mapped to.
            // Default to have minimum 12px and maximum 60px size.
            sizeRange: [parseInt(this.fontMinSize), parseInt(this.fontMaxSize)],
            // Text rotation range and step in degree. Text will be rotated randomly in range [-90,                                                                             90] by rotationStep 45
            //用来调整词的旋转方向，，[0,0]--代表着没有角度，也就是词为水平方向，需要设置角度参考注释内容
            // rotationRange: [-45, 0, 45, 90],
            // rotationRange: [ 0,90],
            rotationRange: [0, 0],
            //随机生成字体颜色
            // maskImage: maskImage,
            //超出范围也会显示
            drawOutOfBound:true,
            textStyle: {
              color: function () {
                // Random color
                return 'rgb(' + [
                  Math.round(Math.random() * 160),
                  Math.round(Math.random() * 160),
                  Math.round(Math.random() * 160)
                ].join(',') + ')';
              }
            },
            //位置相关设置
            // Folllowing left/top/width/height/right/bottom are used for positioning the word cloud
            // Default to be put in the center and has 75% x 80% size.
            left: "center",
            top: "center",
            right: null,
            bottom: null,
            width: "200%",
            height: "200%",
            //数据
            data: this.wordData
          }
        ]
      };
      this.chart.setOption(option);
    }
  }
};
</script>
<style scoped>
</style>
